package com.dongjiantong.biz.serviceImpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.dongjiantong.biz.mapper.EarMarksMapper;
import com.dongjiantong.biz.model.EarMarks;
import com.dongjiantong.biz.service.EarMarksWriteService;
import com.dongjiantong.common.exceptions.HttpCode;
import com.dongjiantong.common.exceptions.OPServerException;
import com.dongjiantong.mybatis.ServiceMybatis;
import lombok.extern.slf4j.Slf4j;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * Created by ThinkPad on 2017/11/14.
 */
@Slf4j
@Service(version = "1.0.0")
public class EarMarksWriteServicImpl extends ServiceMybatis<EarMarks> implements EarMarksWriteService {

    @Resource
    private EarMarksMapper earMarksMapper;


    /**
     * todo 根据主键更新一组耳标
     *
     * @param earMarksList
     * @return
     */
    @Override
    public Boolean updateEarMarkBySome(List<EarMarks> earMarksList) {

        try {

            for (EarMarks earMarks : earMarksList) {
                if (earMarks.getFarm_id() == null) {
                    throw new OPServerException(HttpCode.DATA_UPDATE_FAIL, "《养殖场id不能为空》" + earMarks.toString());
                }
                if (earMarks.getMark_number() == null) {
                    throw new OPServerException(HttpCode.DATA_UPDATE_FAIL, "《耳标号不能为空》" + earMarks.toString());
                }
                earMarksMapper.updateByPrimaryKeySelective(earMarks);

            }
            return true;
        } catch (Exception e) {
            log.error(e.getMessage());
            return false;

        }
    }

    /**
     * 根据耳标号 更新一组耳标或者 一个耳标
     * <p>
     * <set>
     * <if test="isSelected != null">is_selected = #{isSelected},</if>
     * <if test="status != null">status = #{status},</if>
     * <if test="woreAt != null">wore_at = #{woreAt},</if>
     * <if test="outedAt != null">outed_at = #{outedAt}</if>
     * <if test="stableId != null">stable_id = #{breedStableId},</if>
     * <if test="stableNumber != null">stable_number = #{breedStableNumber},</if>
     * </set>
     * <where>
     * <if test="markNumbers != null">AND mark_number IN
     * <foreach collection="markNumbers" open="(" separator="," close=")" item="markNumber">
     * #{markNumber}
     * </foreach>
     * </if>
     * <if test="markNumber!=null">AND mark_number = #{markNumber}</if>
     *
     * @param map
     * @return
     */
    @Override
    public Boolean updateMarks(Map<String, Object> map) {
        return earMarksMapper.updateMarks(map);
    }
}
